//
//  PicImageView.m
//  QSBK
//
//  Created by Eric Wang on 4/23/15.
//  Copyright (c) 2015 Eric Wang. All rights reserved.
//

#import "PicImageView.h"

@implementation PicImageView
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.userInteractionEnabled = YES;
        self.clipsToBounds = NO;
        [self addButton];
    }
    return self;
}

-(void)addButton
{
    UIButton *button = [[UIButton alloc]init];
    
    button.translatesAutoresizingMaskIntoConstraints = NO;
    [button setImage:[UIImage imageNamed:@"delete_image.png"] forState:UIControlStateNormal];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(deleteImage)];
    [button addGestureRecognizer:tap];
    
    [button addTarget:self action:@selector(deleteImage) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    
    NSMutableArray *constraint = [[NSMutableArray alloc]init];
    
    [constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[button(30)]-(-8)-|" options:0 metrics:nil views:@{@"button":button}]];
    [constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(-8)-[button(30)]-(>=0)-|" options:0 metrics:nil views:@{@"button":button}]];
    [self addConstraints:constraint];

    [self addGestureRecognizer:tap];
}

-(void)deleteImage
{
    if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(deleteImage)])
    {
        [self.delegate deleteImage];
    }
}

@end
